var fs = require('fs');
var path = require('path');
var file_read = require('./list_files');
var domain = require('domain');

const reportWorld=require('./module/export/world/report1')

module.exports = function (app) {
		app.get('/', function(req, res, next) {
				res.render('index');
		});

		app.get('/upload', function(req, res, next) {
				res.render('upload');
		});

		

		app.get('/image', function(req, res) {
			   fs.readFile('./logo.png', function(err, data) {
						if (err) throw err;
						//res.writeHead(200, {'Content-Type' : 'image/png' });
						//res.write(data, 'binary');
						res.writeHead(200, {'Content-Type': 'text/html'});
						res.write('<html><body>Image Server<br><img src="data:image/jpeg;base64,')
						res.write(new Buffer(data).toString('base64'));
						res.write('"/>');
						res.write('<br><p>New line</p></body></html>');
				});
		});

		app.get('/download_page', function(req, res, next) {
				res.render('download');
		});

		app.get( '/get_files',function(req,res,next){

			file_read.get_lists(function(files){
				res.send( files );
			}) 
			
		} );
		
		app.get('/download/*', function (req, res, next) {
				target_file = path.join(__dirname, 'public', 'upload', req.params[0]);
				var f = target_file;
				f = path.resolve(f);
				res.download(f);
		});

		app.post('/file-upload', function(req, res, next) {
			var d = domain.create();
			d.on('error', console.error);
			console.log(req.body)
			d.run(function() {
				var file_name = req.body.file_name;
				var target_path;
				if(req.body.pageId){
					target_path= path.join('public', 'upload',req.body.pageId, file_name);
				}else{
					target_path = path.join('public', 'upload', file_name);
				}
				var bitmap = new Buffer(req.body.imgData, 'base64');
    				// write buffer to file
 				//fs.writeFileSync(target_path, bitmap);
				fs.writeFile(target_path, bitmap,function(err){
					console.error(err)
				});
				//specific the upload to path
				//var source = fs.createReadStream(tmp_path);
			 	//var dest = fs.createWriteStream(target_path)
				//source.pipe(dest);
				res.send(file_name);
			});
		});
		app.get('/create-new-page', function (req, res, next) {
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth()+1;
			var day = date.getDate();
			day=day>9?day:"0"+day;
			var hour = date.getHours();
			hour=hour>9?hour:"0"+hour
			var minute = date.getMinutes();
			minute=minute>9?minute:"0"+minute
			var seconds=date.getSeconds();
			seconds=seconds>9?seconds:"0"+seconds
			var pageId= "page-image"+year+month+day+hour+minute+seconds;

			var target_file = path.join(__dirname, 'public', 'upload',pageId);
				var f = target_file;
				f = path.resolve(f);
			fs.mkdir(f,function(err){
				if(err){
					console.warn("create file error:"+f);
					console.log(err)
					res.send({
						code:1,
						message:"create file erryr"
					})
				}else{
					res.send({
						code:0,
						pageId:pageId})
				}
			})
	});
		app.post('/export',function(req, res, next){

			console.log(req.params)
			console.log(req.body)
			var pageId=req.body.pageId;
			if(pageId == undefined){
				pageId=''
			}
			var parameters={}
			if(req.body.tableData){
				parameters.tableData=JSON.parse(req.body.tableData)
			}
			file_read.get_lists(function(images){
				
				parameters.images=[];
				for(var i=0;i<images.length;i++){
					var name=images[i]
					var targetFile=path.join(__dirname,'public','upload',pageId,name)
					
					parameters.images.push(path.resolve(targetFile))
				}
				//parameters=Object.assign(parameters,req.body);
				const fileName= reportWorld.exportWord(res,parameters)

			},pageId)
			
		});

}


